<template>
  <view class="my-userinfo-container">
    <!-- 头像昵称区域 -->
    <view class="top-box">
      <button class="avatar-box" open-type="chooseAvatar" @chooseavatar="onChangeAvatar">
        <image :src="userInfo.avatar" class="avatar"></image>
      </button>
      <view class="nickname">{{userInfo.username}}</view>
    </view>
    <view class="panel-list">
      <!-- 第一个面板 -->
      <!-- <view class="panel">
        <view class="panel-body">
          <view class="panel-item">
            <text>8</text>
            <text>收藏的店铺</text>
          </view>
          <view class="panel-item">
            <text>14</text>
            <text>收藏的商品</text>
          </view>
          <view class="panel-item">
            <text>18</text>
            <text>关注的商品</text>
          </view>
          <view class="panel-item">
            <text>84</text>
            <text>足迹</text>
          </view>
        </view>
      </view> -->
      <!-- 第二个面板（我的订单） -->
      <view class="panel">
        <view class="panel-title">
          <text>我的订单</text>
          <view class="all-order" @click="gotoOrder(0)">
            <text>全部</text>
            <uni-icons type="right" size="12"></uni-icons>
          </view>
        </view>
        <view class="panel-body">
          <view class="panel-item" @click="gotoOrder(1)">
            <image src="@/static/my-icons/icon1.png" class="icon"></image>
            <text>待付款</text>
          </view>
          <view class="panel-item" @click="gotoOrder(2)">
            <image src="@/static/my-icons/send.png" class="icon"></image>
            <text>待发货</text>
          </view>
          <view class="panel-item" @click="gotoOrder(3)">
            <image src="@/static/my-icons/icon2.png" class="icon"></image>
            <text>待收货</text>
          </view>
          <view class="panel-item" @click="gotoOrder(4)">
            <image src="@/static/my-icons/icon3.png" class="icon"></image>
            <text>退款/售后</text>
          </view>
        </view>
      </view>
      <!-- 第三个面板 -->
      <view class="panel">
        <view class="panel-list-item" @click="gotoCollect">
          <text>我的收藏</text>
          <uni-icons type="right" size="15"></uni-icons>
        </view>
        <view class="panel-list-item" @click="chooseAddress">
          <text>收货地址</text>
          <uni-icons type="right" size="15"></uni-icons>
        </view>
        <!--   <view class="panel-list-item">
          <text>联系客服</text>
          <uni-icons type="right" size="15"></uni-icons>
        </view> -->
        <view class="panel-list-item" @click="logout">
          <text>退出登录</text>
          <uni-icons type="right" size="15"></uni-icons>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import { mapState, mapMutations } from 'vuex'
  import { pathToBase64 } from 'image-tools' //引入第三方包，将图片装换为base64格式
  export default {
    name: "my-userinfo",
    data() {
      return {

      };
    },
    computed: {
      ...mapState('m_user', ['userInfo']),
    },
    methods: {
      ...mapMutations('m_user', ['clearAddress', 'updateUserInfo', 'updateToken', 'clearHistory', 'clearCollection',
        'clearOrder', 'updateAddress', 'updateAvatar'
      ]),
      ...mapMutations('m_cart', ['clearCart']),
      // 退出登录
      logout() {
        uni.showModal({
          title: '提示',
          content: '确认退出登录吗？',
          success: (res) => {
            if (res.confirm) {
              this.clearAddress()
              this.updateUserInfo({})
              this.updateToken('')
              // 将vuex仓库中m_cart模块中的购物车数据清空，将本地保存的用户购物车数据清空
              this.clearCart()
              // 将vuex仓库中m_user模块中的搜索历史数据清空，将本地保存的用户搜索历史数据清空
              this.clearHistory()
              // 将vuex仓库中m_user模块中的用户收藏夹数据清空，将本地保存的用户收藏夹数据清空
              this.clearCollection()
              // 清除订单数据
              this.clearOrder()
            }
          },
          fail: (err) => this.$showMsg('退出失败！')
        })
      },
      // 跳转到我的订单页面
      gotoOrder(index) {
        uni.navigateTo({
          url: `/subpkg/order/order?index=${index}`
        })
      },
      // 跳转到我的收藏页面
      gotoCollect() {
        uni.navigateTo({
          url: '/subpkg/collect/collect'
        })
      },
      // 选择收货地址
      async chooseAddress() {
        const [error, success] = await uni.chooseAddress().catch(err => err)
        if (error === null && success.errMsg === 'chooseAddress:ok') {
          this.updateAddress(success)
        }
      },
      // 修改头像
      onChangeAvatar(e) {
        const { avatarUrl } = e.detail
        pathToBase64(avatarUrl)
          .then(base64 => {
            this.updateAvatar(base64)
          })
          .catch(error => {
            console.error(error)
          })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .my-userinfo-container {
    height: 100vh;
    background-color: #F4F4F4;

    .top-box {
      width: 100%;
      height: 400rpx;
      background-color: #C00000;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .avatar-box {
        width: 90px;
        height: 90px;
        padding: 0; //button按钮默认有左右边距，此处重置为0，保证头像居中
        // border: 2px solid #FFF;
        border-radius: 50%;
        box-shadow: 0 1px 5px black;
        background-color: white;
        // box-sizing: border-box;

        .avatar {
          width: 90px;
          height: 90px;
        }
      }

      .nickname {
        font-size: 15px;
        color: #FFF;
        // font-weight: bold;
        margin-top: 10px;
      }
    }

    .panel-list {
      padding: 0 10px;
      position: relative;
      top: -10px;

      .panel {
        background-color: white;
        border-radius: 3px;
        margin-bottom: 8px;

        .panel-title {
          line-height: 45px;
          padding: 0 10px;
          font-size: 15px;
          border-bottom: 1px solid #F4F4F4;
          display: flex;
          justify-content: space-between;

          .all-order {
            font-size: 12px;
            color: gray;
          }
        }

        .panel-body {
          display: flex;
          justify-content: space-around;

          .panel-item {
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: center;
            padding: 10px 0;
            font-size: 13px;

            .icon {
              width: 30px;
              height: 30px;
            }
          }
        }

        .panel-list-item {
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 15px;
          padding: 0 10px;
          line-height: 45px;

          // 为前两个设置下边框
          &:nth-of-type(1),
          &:nth-of-type(2) {
            border-bottom: 1px solid #F4F4F4;
          }
        }
      }
    }
  }
</style>
